import { combineReducers } from 'redux'
import {getMessage} from 'api/index'
// 系统设置 侧边栏
function collapsed(state = false , action) {
  switch (action.type) {
    case 'CHANGE_TAB':
      return !state
    default:
      return state
  }
}
// 个人信息
function userInfo(state = {}, action) {
  switch (action.type) {
    case 'SET_USERINFO':
      return action.info
    case 'REMOVE_USER':
      return {}
    default:
      return state
  }
}
// 顶部历史tab栏
function tabList(state = [], action) {
  switch (action.type) {
    case 'SET_ROUTE':
    if(state.length){
      const  getNumber = async() => {
        const res  = await getMessage()
        taskCount(0,{type:'CHANGE_COUNT',data:res.result})
      }
      getNumber()
    }
      // 判断当前页面是否已经存在  并且替换掉有新参数的路由
      if(state.length>=8){
        state.splice(0,1)
      }
      if(state.some(i=>i.menuName===action.route.menuName)) {
        const data = state.findIndex(i=>i.menuName===action.route.menuName)
        state.splice(data,1,{...action.route,data:state[data].data})
        return [...state]
      }
      return [...state, action.route ]
    case 'CLEAR_ROUTE':
      return []
    case 'FILTER_ROUTE':
      state.splice(action.index,1)
      return state
      case 'SET_DATA':
        console.log(action,state);
      state.filter(i=>i.url===action.url)[0].data = action.data
      return state
    default:
      return state
  }
}

function menus(state=[], action) {
  switch (action.type) {
    case 'UPDATE_ROUTE':
    console.log(action);
    return action.data
    case 'REMOVE_USER':
      return []
    default:
      return state
  }
}

// 审核数据还是已发布数据
function examineStatus(state=false, action) {
  switch (action.type) {
    case 'DATA_PUBLISH':
      return true
    case 'DATA_DRAFT':
        return false
    default:
      return state
  }
}

// 字典数据
function dictionaries(state={},action){
  switch (action.type) {
    case 'SET_DICTIONARIES':
      return action.data
    case 'REMOVE_USER':
      return {}
    default:
        return state
  }
}

// 用户按钮权限
function userBtnRole(state=[],action){
  switch (action.type) {
    case 'SET_ROLES':
      return action.data
    case 'REMOVE_USER':
      return []
    default:
        return state
  }
}
// 用户任务数
function taskCount(state=0,action){
  switch (action.type) {
    case 'CHANGE_COUNT':
      return action.data
    default:
        return state
  }
}


export default combineReducers({
  collapsed,userInfo,tabList,menus,examineStatus,dictionaries,userBtnRole,taskCount
})
